package com.justalk.testapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class Text2View extends View {
    Paint text2Paint;
    String text2Text;
    int ascent;

    //Constructor - for java
    public Text2View(Context context) {
        super(context);
        initialize();
    }

    //Constructor - for xml
    public Text2View(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
        TypedArray ta = context.obtainStyledAttributes(attrs,
                R.styleable.SuperTextView);
        int n = ta.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = ta.getIndex(i);
//            switch (attr) {
////                case R.styleable.SuperTextView:
////                    updateText(ta.getString(R.styleable.SuperTextView));
//                    break;
////TO ADD CUSTOM ATTRIBUTE
//                default:
//                    break;
//            }
        }
        ta.recycle();
    }

    private final void updateText(String s) {
        text2Text = s;
        requestLayout();
        invalidate();
    }

    //load default setting on Text2View
    private final void initialize() {
        text2Paint = new Paint();
        text2Paint.setAntiAlias(true);
        text2Paint.setTextSize(16);
        text2Paint.setColor(0xFF000000);
//setPadding(3, 3, 3, 3);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
    }

    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
            result = specSize;
        } else {
// Measure the text
            result = (int) text2Paint.measureText(text2Text) + getPaddingLeft()
                    + getPaddingRight();
            if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    /**
     * Determines the height of this view
     *
     * @param measureSpec A measureSpec packed into an int
     * @return The height of the view, honoring constraints from measureSpec
     */
    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        ascent = (int) text2Paint.ascent();
        if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
            result = specSize;
        } else {
// Measure the text (beware: ascent is a negative number)
            result = (int) (-ascent + text2Paint.descent()) + getPaddingTop()
                    + getPaddingBottom();
            if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawText(text2Text, getPaddingLeft(), getPaddingTop() - ascent, text2Paint);
    }
}
